#include "mqtttester.h"
#include "ui_mqtttester.h"

MqttTester::MqttTester(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MqttTester)
{
    ui->setupUi(this);
    keyboard = new Keyboard(this);
    keyboard->show();
    this->initMqttClient(ui->edtIp->text(),
                         ui->edtPort->text().toUInt(),
                         "none",
                         "none"
                         );
    this->setupGraph();
    connect(mqttClient,SIGNAL(arrayDataReceived(QList<float> &)),this,SLOT(drawWideGraph(QList<float> &)));
    connect(keyboard,SIGNAL(buttonPressed(QString)),this,SLOT(getPressedBtn(QString)));
}

MqttTester::~MqttTester()
{
    delete ui;
}

void MqttTester::initMqttClient(QString ip, uint port, QString name, QString pass)
{
    mqttClient = new MyMqtt(ip, port, name, pass, this);
}

void MqttTester::setupGraph()
{
    graph = new Graph(this);
    ui->verticalLayout->addWidget(graph->graphView);
}

void MqttTester::drawWideGraph(QList<float> &newData)
{
    QList<QPointF> serie;
    auto size = newData.size();
    auto step = 6000.0 / (float)size;

    serie.resize(size);

    for (uint32_t i=0; i < size; i++) {
        serie[i].setX((float)i + 400.0);
        serie[i].setY(newData.at(i));
    }

    graph->graphSeries[0]->replace(serie);

}

void MqttTester::getPressedBtn(QString btnCaption)
{
    qDebug() << "BTN PRESSED" << btnCaption;
}

